//
//  TYHPassWdViewController.m
//  OpenPractice
//
//  Created by tanyuehong on 8/7/18.
//  Copyright © 2018年  lijinghua. All rights reserved.
//

#import "UIImage+Tint.h"
#import "HyLoginButton.h"
#import "TYHPassWdViewController.h"

@interface TYHPassWdViewController () <UIScrollViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UILabel *placeholderLabel;
@property (nonatomic, strong) UILabel *registerBtn;
@property (nonatomic, strong) UILabel *notMatchLable;
@property (nonatomic, strong) UIButton *clearButton;
@property (nonatomic, strong) HyLoginButton *arrowBtn;   // 点击对号登录

@end

@implementation TYHPassWdViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithHex:0xffffff]];
  //  [self setDefaultBackItemWithBackArrow];
    [self initViews];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledTextChange:) name:UITextFieldTextDidChangeNotification object:nil];
}

#pragma mark - Notification

- (void)changeTextField:(NSNotification *)notification {
    UITextField *textField = [notification object];
    if (self.textField == textField) {
        _placeholderLabel.hidden = self.textField.text.length>0;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  //  [self setNavgationBarHidden:NO animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
   // [self setNavgationBarImage:[UIImage imageWithColor:[UIColor colorWithHex:0xf5f5f5] ]];
    [super viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_textField becomeFirstResponder];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    _textField.delegate = nil;
}

#pragma mark - Private

- (void)setUpNavigationBar {
  //  [self setDefaultBackItemWithBackArrow];
    //    [self setRightItemText:@"忘记密码" target:self action:@selector(lostPassword)];
    [self.navigationItem.leftBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.customView isKindOfClass:[UIButton class]]) {
//            [(UIButton *)obj.customView setImage:[[UIImage imageNamed:@"nav_bar_back.png"] imageWithTintColor:[UIColor colorWithHex:0xffffff]] forState:UIControlStateNormal];
//            [(UIButton *)obj.customView setImage:[[UIImage imageNamed:@"nav_bar_back_pressed.png"] imageWithTintColor:[UIColor colorWithHex:0xdcf4fc]]  forState:UIControlStateSelected];
//            [(UIButton *)obj.customView setImage:[[UIImage imageNamed:@"nav_bar_back_pressed.png"] imageWithTintColor:[UIColor colorWithHex:0xdcf4fc]]  forState:UIControlStateHighlighted];
        }
    }];
    UIButton *button = (UIButton *)self.navigationItem.rightBarButtonItem.customView;
    [button setTitleColor:[UIColor colorWithHex:0xffffff] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithHex:0xdcf4fc] forState:UIControlStateSelected];
    [button setTitleColor:[UIColor colorWithHex:0xdcf4fc] forState:UIControlStateHighlighted];
}

- (void)initViews {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 2)];
    scrollView.delegate = self;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight - kScreenHeight / 3);
    [self.view addSubview:scrollView];
    UILabel *guideLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, kScreenHeight == 480 ? 15.f : 88.f, kScreenWidth - 80, 26)];
    guideLabel.font = [UIFont systemFontOfSize:26];
    guideLabel.textColor = [UIColor colorWithHex:0x333333];
    guideLabel.text = @"请输入密码";
    [scrollView addSubview:guideLabel];
    
    UILabel *subGuideLable = [[UILabel alloc] initWithFrame:CGRectMake(40, guideLabel.bottom + 10, kScreenWidth - 80, 12)];
    subGuideLable.font = [UIFont systemFontOfSize:12.f];
    subGuideLable.textColor = [UIColor colorWithHex:0x333333];
    subGuideLable.text = [NSString stringWithFormat:@"%@已注册，输入密码直接登录",self.phoneNum];
    [scrollView addSubview:subGuideLable];
    
    self.notMatchLable = [[UILabel alloc] initWithFrame:CGRectMake(40, subGuideLable.bottom + 10, kScreenWidth - 80, 12)];
    self.notMatchLable.font = [UIFont systemFontOfSize:12];
    self.notMatchLable.textColor = [UIColor colorWithHex:0xfb7f2d];
    self.notMatchLable.hidden = YES;
    [scrollView addSubview:self.notMatchLable];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(40, self.notMatchLable.bottom + 10, kScreenWidth - 80, 26)];
    self.textField.font = [UIFont systemFontOfSize:26];
    self.textField.keyboardType = UIKeyboardTypeDefault;
    self.textField.textColor = kThemeGreenColor;
    self.textField.textAlignment = NSTextAlignmentLeft;
    self.textField.secureTextEntry = YES;
    self.textField.delegate = self;
    [scrollView addSubview:self.textField];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTextField:) name:UITextFieldTextDidChangeNotification object:_textField];
    
    self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, guideLabel.bottom + 39, kScreenWidth - 80, 26)];
    self.placeholderLabel.center = self.textField.center;
    self.placeholderLabel.font = [UIFont systemFontOfSize:26];
    self.placeholderLabel.textColor = [UIColor colorWithHex:0xd2d2d2];
    self.placeholderLabel.text = @"密码";
    [scrollView addSubview:self.placeholderLabel];
    
    self.clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.clearButton.frame = CGRectMake(self.textField.right - 15, self.textField.top + 5, 15, 15);
    [self.clearButton setImage:[UIImage imageNamed:@"air_class_login_clear_btn"] forState:UIControlStateNormal];
    self.clearButton.hidden = YES;
    [self.clearButton addTarget:self action:@selector(clearPassword) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:self.clearButton];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(40, self.textField.bottom + 20, kScreenWidth - 80, 0.5)];
    lineView.backgroundColor = [UIColor colorWithHex:0xe5e5e5];
    [scrollView addSubview:lineView];
    
    self.arrowBtn = [[HyLoginButton alloc] initWithFrame:CGRectMake(40, lineView.bottom + 30, kScreenWidth - 80, 40)];
    [self.arrowBtn setTitle:@"登录" forState:UIControlStateNormal];
    self.arrowBtn.titleLabel.textColor = [UIColor whiteColor];
    self.arrowBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.arrowBtn.layer.cornerRadius = 5.f;
    [self.arrowBtn addTarget:self action:@selector(clickLoginBtn) forControlEvents:UIControlEventTouchUpInside];
    [self setButtonEnable:NO];
    [scrollView addSubview:_arrowBtn];
    
    UIButton *forgetPassword = [UIButton buttonWithType:UIButtonTypeCustom];
    forgetPassword.frame = CGRectMake(kScreenWidth - 90, self.arrowBtn.bottom + 15, 60, 12);
    [forgetPassword setTitleColor:kThemeGreenColor forState:UIControlStateNormal];
    forgetPassword.titleLabel.font = [UIFont systemFontOfSize:12];
    [forgetPassword setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetPassword addTarget:self action:@selector(lostPassword) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:forgetPassword];
    scrollView.scrollEnabled = NO;
}

- (void)setButtonEnable:(BOOL)enable {
    self.arrowBtn.enabled = enable;
    self.arrowBtn.backgroundColor = enable ? kThemeGreenColor : [UIColor colorWithHex:0x4cc760 andAlpha:0.5];
}

- (void)textFiledTextChange:(NSNotification *)no {
    if (self.textField == no.object) {
        self.notMatchLable.hidden = YES;
        self.placeholderLabel.hidden = self.textField.text.length>0;
        if (self.textField.text.length>=6&&self.textField.text.length<=14) {
            [self setButtonEnable:YES];
            self.clearButton.hidden = NO;
        } else if (self.textField.text.length > 14) {
            self.textField.text = [self.textField.text substringToIndex:14];
            self.clearButton.hidden = NO;
        } else if (self.textField.text.length > 0) {
            [self setButtonEnable:NO];
            self.clearButton.hidden = NO;
        } else {
            [self setButtonEnable:NO];
            self.clearButton.hidden = YES;
        }
    }
}

#pragma mark - Actions

- (void)clickLoginBtn {
    [_textField resignFirstResponder];
    if (self.phoneNum.length>0&&_textField.text.length>0) {
        [[TYHLoginManager sharedInstance] loginWithPhoneNum:self.phoneNum passWd:_textField.text compeletBlock:^(NSError *error, id resultObject) {
            [self dealLoginWithResultObj:resultObject]; // 错误处理
        }];
    }
}

- (void)dealLoginWithResultObj:(NSDictionary *)resultObject {
    NSInteger loginType = [[resultObject objectForKey:@"loginType"] integerValue];
    if (loginType == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)lostPassword {
//    __weak typeof(self) weakSelf = self;
//    ResetPasswordViewController *controller = [[ResetPasswordViewController alloc] init];
//    controller.showTitle = @"找回密码";
//    controller.phoneNum = self.phoneNum;
//    controller.resetSuccess = ^(NSString *pwd){
//        weakSelf.textField.text = pwd;
//        [weakSelf clickLoginBtn];
//        [weakSelf.navigationController popViewControllerAnimated:YES];
//    };
//    [self.navigationController pushViewController:controller animated:YES];
}

- (void)clearPassword {
    self.textField.text = @"";
    self.clearButton.hidden = YES;
    self.placeholderLabel.hidden = NO;
}

- (void)changeToResetPasswordViewWithPhoneNumber:(NSString *)phoneNumber {
//    ResetPasswordViewController *controller = [[ResetPasswordViewController alloc] init];
//    [self.navigationController pushViewController:controller animated:YES];
}


- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

@end
